<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="任务号" prop="spotCheckTaskNumber">
        <el-input
          v-model="queryParams.spotCheckTaskNumber"
          placeholder="请输入任务号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
        <!-- <el-select
          v-model="queryParams.deviceName"
          placeholder="请选择设备名称"
          filterable
          remote
          clearable
          reserve-keyword
          :remote-method="getSelectDeviceNameList"
          :loading="DeviceNameLoading"
          class="!w-220px"
        >
        <el-option
          v-for="item in DeviceNameList"
            :key="item.id"
            :label="item.deviceName"
            :value="item.id"
        />
      </el-select> -->
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select
          v-model="queryParams.deviceType"
          placeholder="请选择设备类型"
          clearable
          class="!w-220px"
        >
        <el-option
            v-for="dict in getIntDictOptions('dict_device_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="configOrgStaffArchivesName">
        <el-input
          v-model="queryParams.configOrgStaffArchivesName"
          placeholder="请输入负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['produce:device-maintenance-task:export']"
        >
        数据导出
        </el-button>
      </div>
      <div>
        <el-button
          type="primary"
          @click="openTableTitleSet"
        >
         表头设置
        </el-button>
      </div>
    </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length?true:false"
      @register="tableoperation"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      :tableHeight="tableHeight"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.pageNo"
      br
      border
      >
      <template #action="{ row }">
        <cell>
          <el-button link type="primary" @click="openForm('update', row.id)">编辑</el-button>
        </cell>
      </template>
    </Table>
  </ContentWrap>
    <!-- 导出 -->
    <ExportFile ref="exportRef" @download="downloadFun" />
    <!-- 表头设置 -->
    <TableWidthSet ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup name="DeviceMaintenanceTask">
import download from '@/utils/download'
import * as DeviceMaintenanceTaskApi from '@/api/produce/deviceManagement/deviceMaintenanceTask'
import {  getIntDictOptions } from '@/utils/dict'
let  tableTitleList = ref([]) // 表头的数据
const loading = ref(true) // 列表的加载中
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  spotCheckTaskNumber: null,
  configOrgStaffArchivesName: null,
  deviceName: null,
  deviceType: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作getUserLabelPage
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject,tableMethods } = useTable({
  getListApi: DeviceMaintenanceTaskApi.getDeviceMaintenanceTaskPage, // 分页接口
  defaultParams:queryParams
})


/**表头数据 */
const getLabelAll = async () => {
  loading.value = true
  try {
    const res = await DeviceMaintenanceTaskApi.getDeviceMaintenanceTaskPage(queryParams)
    res.labels.push({
        label: '操作',
        field: 'action',
        isDetail: false,
        width: 80
    })
    const {allSchemas} = useCrudSchemas(res.labels)
    tableTitleList.value = allSchemas.tableColumns;
    /**表头修改需要刷新表格 */
    getList()
  } finally {
    loading.value = false
  }
}
/** 查询列表 */
const getTableData = (val) => {
  tableObject.currentPage = val
  getList()
}
/**打开表格表头设置**/
const tableTitleSet = ref() 
const openTableTitleSet = () => {
  let code = 'DeviceMaintenanceTask'
  tableTitleSet.value.open(tableTitleList.value, code)
}

// 获得表格的各种操作
const { getList } = tableMethods


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/**表头内部方法 */
const usTableoperation = ref({})
const tableoperation = (table) => {
  usTableoperation.value = table
}

/** 导出按钮操作 */
const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }

  //清空多选
const clearSelectData = () => {
    usTableoperation.value.selections = []
    usTableoperation.value.clearTableSelect()
}

// 下载
const downloadFun = async () => {
    const val = exportRef.value.params
    let params = {
      fileName: val.name,
      ids: ''
    }
    if (val.range === 'all') {
      params.ids = []
    } else {
       params.ids = usTableoperation.value.selections.map( item => item.id)
    }
    DeviceMaintenanceTaskApi.exportDeviceMaintenanceTask(params).then(res=>{
      download.excel(res, val.name)
      clearSelectData()
    })
  }

/** 添加/修改操作 */
const { push } = useRouter() // 路由
const openForm = (type,id) => {
  push({
    path:'/produce/produce/deviceManagement/deviceMaintenanceTask',
    query: {
      id,
      type
    }
  })
}
/** 初始化 **/
const tableHeight = ref(0)
onMounted( async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList() 
})
</script>
<style lang="scss" scoped>
 .btnWrap {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  ::v-deep .el-form-item__content{
    margin-left:12px
  }
  </style>
